﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SuperDarts.Gameplay.Modes;
using SuperDarts.ScreenManagement;
using SuperDarts.Screens.Menus;

namespace SuperDarts.Gameplay.Views
{
    public class CricketView : BaseView
    {
        private Texture2D _bullTexture;
        private Texture2D _closedTexture;
        private SpriteFont _tempFont;
        private readonly Texture2D[] _markTexture = new Texture2D[4];
        private readonly Texture2D[] _numberTextures = new Texture2D[6];
        private const float Scale = 0.8f;

        public CricketView(GameMode mode)
            : base(mode)
        {
        }

        public override void LoadContent(ContentManager content)
        {
            base.LoadContent(content);

            _tempFont = ScreenManager.Trebuchet24;

            for (var i = 0; i < 4; i++)
            {
                _markTexture[i] =
                    content.Load<Texture2D>(@"Images\" + SuperDarts.Options.Theme + @"\" + @"Marks\Mark" + i);
            }

            for (var i = 0; i < 6; i++)
            {
                _numberTextures[i] =
                    content.Load<Texture2D>(@"Images\" + SuperDarts.Options.Theme + @"\" + @"CricketNumbers\" + (i + 15));
            }

            _bullTexture = content.Load<Texture2D>(@"Images\" + SuperDarts.Options.Theme + @"\" + @"CricketNumbers\Bull");
            _closedTexture =
                content.Load<Texture2D>(@"Images\" + SuperDarts.Options.Theme + @"\" + @"CricketNumbers\Closed");
        }

        public override void DrawDartScore(SpriteBatch spriteBatch)
        {
            var dartSize = new Vector2(DartTexture.Width, DartTexture.Height)*1.4f;
            var y = (int) (SuperDarts.Viewport.Height*0.5f - (Mode.DartsPerRound*dartSize.Y*0.5f));
            base.DrawDartScore(spriteBatch, dartSize, new Vector2(SuperDarts.Viewport.Width*0.75f, y), true);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);

            spriteBatch.Begin();

            //Draw round scores
            DrawRoundMarks(spriteBatch);

            var position = new Vector2(SuperDarts.Viewport.Width*0.5f, SuperDarts.Viewport.Height*0.1f);
            var scale = 0.6f;

            var tempColor = Color.Black;
            tempColor.A = 80;

            //Draw panels for marks
            spriteBatch.Draw(ScreenManager.BlankTexture,
                new Rectangle((int) (position.X - _markTexture[0].Width*1.5f*scale),
                    (int) (position.Y - _markTexture[0].Height*0.5f*scale),
                    (int) (_markTexture[0].Width*scale),
                    (int) (_markTexture[0].Height*7*scale)), tempColor);

            spriteBatch.Draw(ScreenManager.BlankTexture,
                new Rectangle((int) (position.X + _markTexture[0].Width*0.5f*scale),
                    (int) (position.Y - _markTexture[0].Height*0.5f*scale),
                    (int) (_markTexture[0].Width*scale),
                    (int) (_markTexture[0].Height*7*scale)), tempColor);

            var temp = Vector2.Zero;

            //Draw marks
            for (var i = 20; i >= 15; i--)
            {
                DrawMarks(spriteBatch, position, i);
                position.Y += _markTexture[0].Height*scale;
            }

            // Same thing for the bulls eye
            temp = position - new Vector2(_markTexture[0].Width*1.5f*scale, _markTexture[0].Height*0.5f*scale);
            DrawMarks(spriteBatch, position, 25, "BULL", Color.Red);

            spriteBatch.End();
        }

        private void DrawRoundMarks(SpriteBatch spriteBatch)
        {
            var position = new Vector2(20, SuperDarts.Viewport.Height*0.4f);
            var offset = Vector2.Zero;
            var scale = 0.25f;
            var spacing = _markTexture[0].Width*scale + 10.0f;

            for (var i = 0; i < Mode.CurrentPlayer.Rounds.Count; i++)
            {
                var round = Mode.CurrentPlayer.Rounds[i];
                var c = Color.White;

                if (i == Mode.CurrentRoundIndex)
                    c = Color.Yellow;

                var text = "R" + (i + 1) + ": ";

                var textSize = _tempFont.MeasureString(text);
                offset = new Vector2(_markTexture[0].Width, _markTexture[0].Height)*0.5f*scale;
                var debug = "";
                for (var j = 0; j < round.Darts.Count; j++)
                {
                    var scoredMarks = (Mode as Cricket).ScoredMarks[round.Darts[j]];
                    debug += scoredMarks.ToString();

                    if (j < round.Darts.Count - 1)
                        debug += ", ";

                    spriteBatch.Draw(_markTexture[Math.Min(scoredMarks, 3)],
                        position + textSize*new Vector2(1, 0.5f) - offset + Vector2.UnitX*spacing*j +
                        Vector2.UnitX*10.0f, null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }

                if (SuperDarts.Options.Debug)
                    text += debug;

                TextBlock.DrawShadowed(spriteBatch, _tempFont, text, c, position);

                position.Y += _tempFont.MeasureString(text).Y;
            }
        }

        private void DrawMarks(SpriteBatch spriteBatch, Vector2 position, int i, string label, Color color)
        {
            var temp = position - new Vector2(_markTexture[0].Width*1.5f*Scale, _markTexture[0].Height*0.5f*Scale);
            var offset = Vector2.Zero;
            Color c;

            for (var j = 0; j < SuperDarts.Players.Count; j++)
            {
                //draw marks
                var marks = (Mode as Cricket).Segments[i].Marks[j];

                c = Color.White;

                if (!(Mode as Cricket).Segments[i].IsAlive)
                {
                    c = Color.White*0.33f;
                }

                if (marks > 0)
                    spriteBatch.Draw(_markTexture[Math.Min(marks, 3)], temp, null, c, 0, Vector2.Zero, Scale,
                        SpriteEffects.None, 0);

                var center = new Vector2(_markTexture[0].Width, _markTexture[0].Height)*Scale*0.5f;
                offset = _tempFont.MeasureString(marks.ToString())*0.5f;

                if (SuperDarts.Options.Debug)
                {
                    spriteBatch.DrawString(_tempFont, marks.ToString(), temp + center - offset + Vector2.One,
                        Color.Black);
                    spriteBatch.DrawString(_tempFont, marks.ToString(), temp + center - offset, Color.White);
                }

                temp.X += _markTexture[0].Width*Scale*2f;
            }

            if (SuperDarts.Options.Debug)
            {
                offset = _tempFont.MeasureString(label)*0.5f;
                spriteBatch.DrawString(_tempFont, label, position - offset + Vector2.One, Color.Black);
                spriteBatch.DrawString(_tempFont, label, position - offset, color);
            }

            offset = new Vector2(_numberTextures[0].Width, _numberTextures[0].Height)*0.5f;
            Texture2D tex;

            if (i != 25)
                tex = _numberTextures[i - 15];
            else
                tex = _bullTexture;

            c = Color.White;

            if (!(Mode as Cricket).Segments[i].IsAlive)
            {
                c = Color.White*0.33f;
            }

            spriteBatch.Draw(tex, position - offset, c);

            if (!(Mode as Cricket).Segments[i].IsAlive)
            {
                offset = new Vector2(_closedTexture.Width, _closedTexture.Height)*0.5f;
                spriteBatch.Draw(_closedTexture, position - offset, Color.White);
            }
        }

        private void DrawMarks(SpriteBatch spriteBatch, Vector2 position, int i)
        {
            DrawMarks(spriteBatch, position, i, i.ToString(), Color.White);
        }
    }
}